package br.com.winesys.geta.databinding.component.selector;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileFilter;
import br.com.winesys.geta.databinding.component.listeners.SelectionListener;
import com.l2fprod.common.swing.JDirectoryChooser;

public final class FolderSelector extends JPanel {

	private JButton button;
	private JDirectoryChooser directoryChooser;
	private JTextField text;
	private File file;

	public FolderSelector(int dialogType) {
		this.setFocusable(false);
		this.directoryChooser = new JDirectoryChooser();
		this.directoryChooser.setDialogType(dialogType);
		for (Component p : this.directoryChooser.getComponents()) {
			if (p instanceof JPanel) {
				JPanel panel = (JPanel) p;
				for (Component c : panel.getComponents()) {
					if (c instanceof JButton) {
						if (((JButton) c).getText().startsWith("Make")) {
							panel.remove(c);
							break;
						}
					}
				}
			}
		}
		this.text = new JTextField();
		this.text.setEditable(false);
		this.text.setFocusable(false);
		this.button = new JButton();
		this.button.setIcon(new ImageIcon(getClass().getResource("/images/folder.png")));
		this.button.setFocusable(false);
		this.button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent evt) {
				select();
			}
		});

		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addComponent(this.text, GroupLayout.PREFERRED_SIZE, 300, Short.MAX_VALUE)
						.addComponent(this.button, GroupLayout.PREFERRED_SIZE, this.text.getPreferredSize().height,
								this.text.getPreferredSize().height)));
		layout.setVerticalGroup(layout.createParallelGroup(Alignment.LEADING).addComponent(this.text)
				.addComponent(this.button, 0, 0, Short.MAX_VALUE));
	}

	public FolderSelector(int dialogType, FileFilter filtro) {
		this(dialogType);
		this.directoryChooser.setAcceptAllFileFilterUsed(false);
		this.directoryChooser.setFileFilter(filtro);
	}

	public void setFile(File file) {
		this.file = file;
		try {
			if (file != null) {
				this.text.setText(file.getCanonicalPath());
			} else {
				this.text.setText("");
			}
			this.text.setCaretPosition(0);
		} catch (IOException ex) {
			Logger.getLogger(FolderSelector.class.getName()).log(Level.SEVERE, null, ex);
		}
		for (SelectionListener<File> listener : this.listenerList.getListeners(SelectionListener.class)) {
			listener.itemSelected(this.file);
		}
	}

	public File getFile() {
		return this.file;
	}

	private void select() {
		this.directoryChooser.setSelectedFile(this.file);
		if (this.directoryChooser.showOpenDialog(SwingUtilities.getWindowAncestor(this)) == JFileChooser.APPROVE_OPTION) {
			this.setFile(this.directoryChooser.getSelectedFile());
		}
	}

	public void addSelectionListener(SelectionListener<File> listener) {
		this.listenerList.add(SelectionListener.class, listener);
	}

	public void removeSelectionListener(SelectionListener<File> listener) {
		this.listenerList.remove(SelectionListener.class, listener);
	}

	@Override
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		for (Component c : this.getComponents()) {
			c.setFocusable(enabled);
			c.setEnabled(enabled);
		}
	}
}
